package com.cci.kangdao.service.impl;


import com.cci.kangdao.aop.UserThreadLocal;
import com.cci.kangdao.dao.BusinessRegionTDao;
import com.cci.kangdao.dao.NotifyRecordDYDao;
import com.cci.kangdao.dao.model.UserT;
import com.cci.kangdao.service.NotifyRecordDYTService;
import com.cci.kangdao.service.PropertiesUtils;
import com.cci.kangdao.utilTool.ApiErrorCode;
import com.cci.kangdao.utilTool.LocalTimeUtil;
import com.cci.kangdao.utilTool.ParameterTool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("NotifyRecordDYTService")
public class NotifyRecordDYTServiceImpl implements NotifyRecordDYTService {

	private Logger log = Logger.getLogger(this.getClass().getName());
	@Resource
	private NotifyRecordDYDao notifyRecordDYDao;
	@Resource
	private PropertiesUtils propertiesUtils;

	@Override
	public ModelAndView getNotifyRecordDYList(Map<String, Object> map) {
		HttpServletResponse response = (HttpServletResponse) map.get("response");
		UserT userT = UserThreadLocal.get();
		JSONObject obj = new JSONObject();  //声明返给前端json对象
		String roleNo = MapUtils.getString(map,"RoleNo","");
		//处理分页信息
		int page = 1;
		if (map.get("page") != null) {
			page = MapUtils.getInteger(map, "page", 1);
		}
		int pagesize = Integer.valueOf(this.propertiesUtils.getPropertiesValue("pagesize"));  //每页显示多少条数据;
		try {
			if(StringUtils.isEmpty(roleNo)){
				obj.put("data", "");
				obj.put("status", -5);
				obj.put("msg", ApiErrorCode.getInstance().getErrorMsg(-5));
			} else {
				if("R0002".equals(roleNo)){   //用户消息列表
					//分页处理，显示第一页的10条数据
					PageHelper.startPage(page, pagesize);
					List<Map<String,Object>> custmerMsgList = notifyRecordDYDao.getCustmerMsgList(userT.getId());
					Map<String,Object> custmerMsgListMap = new HashMap<>();
					custmerMsgListMap.put("msgList",custmerMsgList);
					obj.put("data", custmerMsgListMap);
					obj.put("status", 0);
					obj.put("msg", "success");
				}else if("R0041,R0069".contains(roleNo)){   //服务站站长，信息员消息列表
					PageHelper.startPage(page, pagesize);
					List<Map<String,Object>> otherMsgList = notifyRecordDYDao.getServiceStationMsgList(userT.getId(),userT.getServiceStationID());
					Map<String,Object> otherMsgListMap = new HashMap<>();
					otherMsgListMap.put("msgList",otherMsgList);
					obj.put("data", otherMsgListMap);
					obj.put("status", 0);
					obj.put("msg", "success");
				}else if("R0095,R0096,R0097".contains(roleNo)){   //区域经理，400客服专员，区域leader消息列表
					PageHelper.startPage(page, pagesize);
					List<Map<String,Object>> otherMsgList = notifyRecordDYDao.getLeaderMsgList(userT.getId());
					Map<String,Object> otherMsgListMap = new HashMap<>();
					otherMsgListMap.put("msgList",otherMsgList);
					obj.put("data", otherMsgListMap);
					obj.put("status", 0);
					obj.put("msg", "success");
				}else{   //未知用户角色
					obj.put("data", "");
					obj.put("status",-40 );
					obj.put("msg", "未知用户角色");
				}
			}
			ParameterTool.writeResponse(response, obj.toString());
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			ParameterTool.writeErrorResponse(response);
			// 主动回滚事务
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return null;
	}

	@Override
	public ModelAndView markReadDYNotifyRecordByOrderId(Map<String, Object> map) {
		HttpServletResponse response = (HttpServletResponse) map.get("response");
		UserT userT = UserThreadLocal.get();
		JSONObject obj = new JSONObject();  //声明返给前端json对象
		String roleNo = MapUtils.getString(map,"RoleNo","");
		long orderId = MapUtils.getLongValue(map,"AppointmentOrderID");  //预约订单id
		long workOrderId = MapUtils.getLongValue(map,"AppointmentWorkOrderID");  //预约工单id
		try {
			if(StringUtils.isEmpty(roleNo)){
				obj.put("data", "");
				obj.put("status", -5);
				obj.put("msg", ApiErrorCode.getInstance().getErrorMsg(-5));
			} else {
				if("R0002,R0095,R0096,R0097".contains(roleNo)){   //用户,区域经理，400客服专员，区域leader查看订单消息列表
					//根据订单id，工单id，用户id获取用户所有消息列表
					List<Map<String,Object>> dyNotifyRecordList = notifyRecordDYDao.getYHDYNotifyRecordList(orderId,userT.getId());
					//获取完消息立马标志已读
					int markReadDYNotifyRecordByOrderIdCount = notifyRecordDYDao.markReadYHDYNotifyRecordByOrderId(userT.getId(), LocalTimeUtil.getRealCurrentTime(),orderId);
					Map<String,Object> returnMap = new HashMap<>();
					returnMap.put("dyNotifyRecordList",dyNotifyRecordList);
					obj.put("data", returnMap);
					obj.put("status", 0);
					obj.put("msg", "success");
				}else if("R0041,R0069".contains(roleNo)){   //服务站站长，信息员查看工单消息列表
					//根据订单id，工单id，用户id获取用户所有消息列表
					List<Map<String,Object>> dyNotifyRecordList = notifyRecordDYDao.getDYNotifyRecordList(orderId,workOrderId,userT.getId());
					//获取完消息立马标志已读
					int markReadDYNotifyRecordByOrderIdCount = notifyRecordDYDao.markReadDYNotifyRecordByOrderId(userT.getId(), LocalTimeUtil.getRealCurrentTime(),orderId, workOrderId);
					Map<String,Object> returnMap = new HashMap<>();
					returnMap.put("dyNotifyRecordList",dyNotifyRecordList);
					obj.put("data", returnMap);
					obj.put("status", 0);
					obj.put("msg", "success");
				}else{   //未知用户角色
					obj.put("data", "");
					obj.put("status",-40 );
					obj.put("msg", "未知用户角色");
				}
			}
			ParameterTool.writeResponse(response, obj.toString());
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			ParameterTool.writeErrorResponse(response);
			// 主动回滚事务
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return null;
	}
}
